/**
 * Copyright (C) 2014 xspace app Project.
 */

package cn.xspace.module.other.http.impl;

import android.content.Context;
import android.widget.Toast;

import com.xspace.android.xafcommon.network.NetConstant;
import com.xspace.android.xafcommon.network.message.AResponseMsg;
import com.xspace.android.xafcommon.network.message.SendRequestListener;
import com.xspace.android.xafcommon.util.ResourceUtils;

import cn.xspace.module.other.R;
import cn.xspace.module.other.activity.ReSetPwdAct;
import cn.xspace.module.other.http.NetControl;
import cn.xspace.module.other.http.intface.IResetPassword;
import cn.xspace.module.other.http.resp.RegisterResp;
import cn.xspace.module.other.http.resp.ResetPasswordResp;
import cn.xspace.module.other.util.RProxy;

/**
 * @version V1.0.0
 * @ClassName: RegisterImpl
 * @author:mawen
 * @Date：2014-2-25
 * @Description: TODO
 */
public class ResetPasswordImpl implements SendRequestListener, IResetPassword {
    private NetControl netControl;
    private Context mContext;
    private IResetPasswordMg mCallBack;

    /**
     * @param
     * @return
     * @throws
     * @Description: TODO
     */
    public ResetPasswordImpl(Context context) {
        this.mContext = context;
        netControl = new NetControl(this);
    }

    @Override
    public void reSetPassword(String mobile, String pwd, String oldPwd, IResetPasswordMg callBack) {
//		((ReSetPwdAct) mContext).showDialog(mContext.getString(RProxy.dialog_submit));; //正在提交
        ((ReSetPwdAct) mContext).showWaitDialog(ResourceUtils.getString(R.string.dialog_submit));
        netControl.sendResetPasswordReq(mobile, pwd, oldPwd);
        this.mCallBack = callBack;
    }

    @Override
    public void requestResult(int result, AResponseMsg object) {
        String errorTip = "";
        ((ReSetPwdAct) mContext).hideWaitDialog();
        if (object.msgType.equals(NetControl.MSGTYPE_RESETPASSWORD)) {
            if (result == NetConstant.TaskStatus.SUCCESS) {
                ResetPasswordResp resp = (ResetPasswordResp) object;
                if (resp.findPasswordBean.code.equals(String
                        .valueOf(ResetPasswordResp.FINDPASSWORD_SUCCESS))) {// 找回密码成功
                    errorTip = mContext
                            .getString(RProxy.registeract_resetpwd_success);
                    Toast.makeText(mContext, errorTip, Toast.LENGTH_LONG).show();
                    mCallBack.onCallBack(true);
                } else if (resp.findPasswordBean.code.equals(String
                        .valueOf(RegisterResp.REGISTER_VERTIFYCODE_ERROR))) {// 找回密码验证码错误
                    errorTip = mContext
                            .getString(RProxy.registeract_register_vertifycode_error);
                    Toast.makeText(mContext, errorTip, Toast.LENGTH_LONG).show();
                    mCallBack.onCallBack(false);
                } else {// 找回密码失败
                    errorTip = mContext
                            .getString(RProxy.registeract_resetpwd_error);
                    Toast.makeText(mContext, errorTip, Toast.LENGTH_LONG).show();
                    mCallBack.onCallBack(false);
                }
            } else {
                errorTip = mContext.getString(RProxy.network_fail);
                Toast.makeText(mContext, errorTip, Toast.LENGTH_LONG).show();
                mCallBack.onCallBack(false);
            }
            //Toast.makeText(mContext, errorTip, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void offline(String msgType) {
        ((ReSetPwdAct) mContext).hideWaitDialog();
        Toast.makeText(mContext, RProxy.network_fail, Toast.LENGTH_SHORT)
                .show();
    }

    public interface IResetPasswordMg {
        public void onCallBack(boolean flag);
    }
}
